<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/2/23
 * Time: 14:48
 */
class Model_Index extends PhalApi_Model_NotORM{
    protected function getTableName($id){
        return 'p_user';
    }
    public function getByUser(){
        return $this->getORM()->select('*')->fetchAll();
    }
    /*
     * 发送用户信息  手机号码入库
     */
    public function getByPhone($obj){
        $time=time();
        //$_SESSION['phone'] = $obj->phone;   //把手机号码存入session
        $data=array(
            'landtime' => $time,
            'phone'  => $obj->phone,
        );
        $this->getORM()->insert($data);
    }
    //查询手机号当天 是否已经插入
    public function isPhone($obj){
        $time = date('Ymd',time());
        $phone=$obj->phone;
        //return $phone;
        return $this->getORM()->select('*')->where("phone=? and FROM_UNIXTIME(landtime,'%Y%m%d')=$time",$phone)->fetchAll();
        //$sql="select * from p_user where phone = $phone and FROM_UNIXTIME(landtime,'%Y%m%d') like $time";
        //$this->getORM()->queryAll($sql,array());
    }
    /*
     * 点击投票按钮，当前用户当天是否投满三家，投票权限    等于0没有权限
     */
    public function getCurrentPhone($obj){
        $time = date('Ymd',time());
        return $this->getORM()->select('*')->where("phone=? and FROM_UNIXTIME(landtime,'%Y%m%d')=$time and vote=?",$obj->phone,0)->fetchRow();
    }
    public function getCurrentPhone1($obj){
        $time = date('Ymd',time());
        return $this->getORM()->select('*')->where("phone=? and FROM_UNIXTIME(landtime,'%Y%m%d')=$time and vote>?",$obj->phone,0)->fetchRow();
    }
    /**
     * 分享后投票次数加一
     */
    public function doShare($obj){
        $phone=$obj->phone;
        $this->getORM()->where('phone', $phone)->update(array('vote' => new NotORM_Literal("vote + 1")));
    }

    /**
     * 判断是否已经分享过
     */
    public function isShared($obj){
        $time = date('Ymd',time());
        $phone=$obj->phone;
        return $this->getORM()->select('share')->where("phone=? and FROM_UNIXTIME(landtime,'%Y%m%d')=$time",$phone)->fetchRow();
    }
    /*
     * 投票  用户投票减1， 抽奖机会加1
     */
    public function setVote($obj){
        $time = date('Ymd',time());
        $phone=$obj->phone;
        // DI()->notorm->transaction = 'BEGIN';
        return $this->getORM()->where("phone=? and FROM_UNIXTIME(landtime,'%Y%m%d')=$time",$phone)
               ->update(array('vote' => new NotORM_Literal("vote - 1"),'lottery'=>new NotORM_Literal("lottery + 1")));

        /*  $res = $this->getORM()->where("phone=? and FROM_UNIXTIME(landtime,'%Y%m%d')=$time",$phone)->update(array('vote' => new NotORM_Literal("vote - 1"),'lottery'=>new NotORM_Literal("lottery + 1")));
          if(!$res){
              DI()->notorm->transaction = 'ROLLBACK';
              return 1;
          }

          //商家
          $model = new Model_Client();
          $res = $model -> clientVote($obj);
          if(!$res){
              DI()->notorm->transaction = 'ROLLBACK';
              return 1;
          }
          DI()->notorm->transaction = 'COMMIT';
          return 0;*/
    }
    /**
     * 用户当天抽奖    判断用户当天是否中过奖
     */
    public function getGift($obj){
        $phone = $obj->phone;
        $time = date('Ymd',time());
        return $this->getORM()->select('*')->where("phone=? and prizetime=$time and FROM_UNIXTIME(landtime,'%Y%m%d')=$time",$phone)->fetchAll();
    }
    /*
     * 用户中奖把中奖时间当天入库
     */
    public function writePrize($obj){
        $phone = $obj->phone;
        $time = date('Ymd',time());
        $this->getORM()->where("phone=? and FROM_UNIXTIME(landtime,'%Y%m%d')=$time",$phone)
            ->update(array('prizetime'=>$time));
    }
    /*
     * 判断用户当天是否有中奖机会
     */
    public function isChance($obj){
        $phone = $obj->phone;
        $time = date('Ymd',time());
       return $this->getORM()->select('lottery')->where("phone=? and FROM_UNIXTIME(landtime,'%Y%m%d')=$time",$phone)
              ->fetchAll();
    }
    /*
     * 用户中奖，机会减 1
     */
    public function decLottery($obj){
        $phone = $obj->phone;
        $time = date('Ymd',time());
        $this->getORM()->where("phone=? and FROM_UNIXTIME(landtime,'%Y%m%d')=$time and prizetime=$time",$phone)
             ->update(array('lottery'=>new NotORM_Literal("lottery - 1")));
    }
}